
package com.xifan.myaccount.util;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.xifan.myaccount.database.DatabaseImpl;
import com.xifan.myaccount.entity.Account;
import com.xifan.myaccount.entity.RecordType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class SmartType {

    public static final int TYPE_ALL = 0;
    public static final int TYPE_EXPEND = 1;
    public static final int TYPE_REVENUE = 2;

    private static final int MARK_TO_TOP = 90;

    private static Context mContext = ContextUtil.getContext();

    @SuppressWarnings("unchecked")
    public static List<Account> getAccountList() {
        return new DatabaseImpl(mContext).doQuery(DatabaseImpl.DB_TABLE_ACCOUNT, "", false);
    }

    @SuppressWarnings("unchecked")
    public static List<String> getAccountTypeList() {
        return new DatabaseImpl(mContext).doQuery(DatabaseImpl.DB_TABLE_ACCOUNT_TYPE, "", false);
    }

    /**
     * 排序逻辑：用mark来标记，首先初始值根据freq排列，如果last_date小于三天则
     * mark++，然后根据event_stamp，有其中一个则mark++。
     * 
     * @return
     */
    public static List<RecordType> getMatch(int opType) {
        // get types by frequency order
        List<RecordType> headList = new ArrayList<RecordType>();
        List<RecordType> list = getTypeList(opType);

        int mark;
        int hour = -1;
        String[] stamp;
        RecordType type;
        for (int i = 0, s = list.size(); i < s; i++) {
            type = list.get(i);
            mark = 0;
            // Start check event_stamp
            if (!TextUtils.isEmpty(type.stamp)) {
                stamp = type.stamp.split(",");
                for (String str : stamp) {
                    if (str.indexOf("h") > -1) {
                        hour = Util.getHourOfTime() - Integer.parseInt(str.replace("h", "").trim());
                        if (hour > -1 && hour < 4) {
                            mark = MARK_TO_TOP;
                            Log.e("search", "hour match! is " + hour);
                        }
                    } else if (str.indexOf("d") > -1) {
                        if (Integer.parseInt(str.replace("d", "").trim()) == Util.getDayOfTime()) {
                            mark = MARK_TO_TOP;
                            Log.e("search",
                                    "day match! is "
                                            + Integer.parseInt(str.replace("d", "").trim()));
                        }
                    } else if (str.indexOf("m") > -1) {
                        if (Integer.parseInt(str.replace("m", "").trim()) == Util.getMonth()) {
                            mark = MARK_TO_TOP;
                            Log.e("search",
                                    "month match! is "
                                            + Integer.parseInt(str.replace("m", "").trim()));
                        }
                    }
                }
            }
            // Start check last_date
            if (Util.getDaysFromNow(type.lastDate) > 1) {
                mark++;
            }

            // save weight to item
            type.weight = mark;
            // move weight item to headlist
            if (mark > 0) {
                headList.add(type);
            }
        }
        // remove items that moved to headlist
        for (int i = 0, s = headList.size(); i < s; i++) {
            list.remove(headList.get(i));
        }

        Collections.sort(headList);
        headList.addAll(list);

        // so here we got headlist sort by weight and the rest order in freq.
        return headList;
    }

    @SuppressWarnings("unchecked")
    public static List<RecordType> getTypeList(int opType) {
        DatabaseImpl db = new DatabaseImpl(mContext);
        String sql = null;
        if (opType == 0) {
            sql = "order by freq desc";
        } else {
            sql = "where operate_type=" + opType + " order by freq desc";
        }
        return db.doQuery(DatabaseImpl.DB_TABLE_TYPE, sql, false);
    }

    public static int getTypeId(String typeName) {
        List<RecordType> typeList = getTypeList(0);
        RecordType type;
        for (Iterator<RecordType> i = typeList.iterator(); i.hasNext();) {
            type = i.next();
            if (type.typeName.equals(typeName)) {
                return type.typeId;
            }
        }
        return -1;
    }

    public static RecordType getTypeInfoById(int typeId) {
        return getTypeInfoById(getTypeList(0), typeId);
    }

    public static RecordType getTypeInfoById(int opType, int typeId) {
        return getTypeInfoById(getTypeList(opType), typeId);
    }

    public static RecordType getTypeInfoById(List<RecordType> list, int typeId) {
        List<RecordType> typeList = list;
        RecordType type;
        for (Iterator<RecordType> i = typeList.iterator(); i.hasNext();) {
            type = i.next();
            if (type.typeId == typeId) {
                return type;
            }
        }
        return null;
    }

    public static String[] getPinyinList(List<RecordType> list) {
        String[] newArray = new String[list.size()];
        for (int i = list.size() - 1; i >= 0; i--) {
            newArray[i] = list.get(i).typePinyin;
        }
        return newArray;
    }
}
